<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-BaseHTTPServer.html" />
<link rel="prev" href="module-urlparse.html" />
<link rel="parent" href="internet.html" />
<link rel="next" href="node632.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>18.18 SocketServer -- A framework for network servers</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="18.17.1 results of urlparse()"
  href="urlparse-result-object.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="18. internet Protocols and"
  href="internet.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="18.18.1 server Creation Notes"
  href="node632.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="urlparse-result-object.html">18.17.1 Results of urlparse()</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="internet.html">18. Internet Protocols and</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node632.html">18.18.1 Server Creation Notes</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION00201800000000000000000">
18.18 <tt class="module">SocketServer</tt> --
         A framework for network servers</a>
</h1>

<p>
<a name="module-SocketServer"></a>

<p>
The <tt class="module">SocketServer</tt> module simplifies the task of writing network
servers.

<p>
There are four basic server classes: <tt class="class">TCPServer</tt> uses the
Internet TCP protocol, which provides for continuous streams of data
between the client and server.  <tt class="class">UDPServer</tt> uses datagrams, which
are discrete packets of information that may arrive out of order or be
lost while in transit.  The more infrequently used
<tt class="class">UnixStreamServer</tt> and <tt class="class">UnixDatagramServer</tt> classes are
similar, but use <span class="Unix">Unix</span> domain sockets; they're not available on
non-<span class="Unix">Unix</span> platforms.  For more details on network programming, consult
a book such as W. Richard Steven's <em class="citetitle"
 >UNIX Network Programming</em>
or Ralph Davis's <em class="citetitle"
 >Win32 Network Programming</em>.

<p>
These four classes process requests <i class="dfn">synchronously</i>; each request
must be completed before the next request can be started.  This isn't
suitable if each request takes a long time to complete, because it
requires a lot of computation, or because it returns a lot of data
which the client is slow to process.  The solution is to create a
separate process or thread to handle each request; the
<tt class="class">ForkingMixIn</tt> and <tt class="class">ThreadingMixIn</tt> mix-in classes can be
used to support asynchronous behaviour.

<p>
Creating a server requires several steps.  First, you must create a
request handler class by subclassing the <tt class="class">BaseRequestHandler</tt>
class and overriding its <tt class="method">handle()</tt> method; this method will
process incoming requests.  Second, you must instantiate one of the
server classes, passing it the server's address and the request
handler class.  Finally, call the <tt class="method">handle_request()</tt> or
<tt class="method">serve_forever()</tt> method of the server object to process one or
many requests.

<p>
When inheriting from <tt class="class">ThreadingMixIn</tt> for threaded connection
behavior, you should explicitly declare how you want your threads
to behave on an abrupt shutdown. The <tt class="class">ThreadingMixIn</tt> class
defines an attribute <var>daemon_threads</var>, which indicates whether
or not the server should wait for thread termination. You should
set the flag explicitly if you would like threads to behave
autonomously; the default is <tt class="constant">False</tt>, meaning that Python
will not exit until all threads created by <tt class="class">ThreadingMixIn</tt> have
exited.

<p>
Server classes have the same external methods and attributes, no
matter what network protocol they use:

<p>

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="node632.html">18.18.1 Server Creation Notes</a>
<li><a href="node633.html">18.18.2 Server Objects</a>
<li><a href="node634.html">18.18.3 RequestHandler Objects</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="18.17.1 results of urlparse()"
  href="urlparse-result-object.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="18. internet Protocols and"
  href="internet.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="18.18.1 server Creation Notes"
  href="node632.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="urlparse-result-object.html">18.17.1 Results of urlparse()</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="internet.html">18. Internet Protocols and</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node632.html">18.18.1 Server Creation Notes</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
